package com.yuwei.interceptor;

import com.mysql.jdbc.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 拦截器中需要取出header中的token，然后去redis中进行判断，如果存在，则允许操作，则返回提示信息
 */
public class AuthInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        String token = request.getHeader("Authorization");
        if (StringUtils.isNullOrEmpty(token)) {
            return false;
        }
        //通过redis检查token的有效性
        if ("yuwei".equals(token)){
            return true;
        }else{
            System.out.println("token验证不成功,获取的token:" + token);
            return false;
        }

    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
